package sfin.controller;

import sfin.service.BancoService;
import sfin.service.ContaService;
import sfin.swt.exception.StatizoException;
import br.com.statizo.financeiro.domain.entity.impl.ContaCorrente;

public class ContaController {

	private String apelido = "";
	private String agencia = "";
	private String conta = "";
	private int banco = -1;
	private boolean padrao;
	
	private ContaService service;
	private BancoService bancoService;
	
	private int selectedIndex = -1;
	
	private ControllerUtil util;
	
	public ContaController(ContaService service, BancoService bancoService, ControllerUtil util) {
		this.service = service;
		this.bancoService = bancoService;
		this.util = util;
	}
	
	public void callSave() throws StatizoException{
		selectedIndex = service.save(selectedIndex, apelido, agencia, conta, util.normalizeOptionToSave(banco), padrao);
		loadData();
	}
	
	public void clearSelection(){
		this.selectedIndex = -1;
		loadData();
	}
	
	private void loadData() {
		if(selectedIndex >= 0){
			ContaCorrente cc = service.getAt(selectedIndex);
			apelido = cc.getApelido();
			agencia = cc.getAgencia();
			conta = cc.getNumero();
			banco = bancoService.indexOf(cc.getBanco());
			padrao = cc.isPadrao();
		}else{
			apelido = "";
			agencia = "";
			conta = "";
			banco = -1;
			padrao = false;
		}
	}

	public String getApelido() {
		return apelido;
	}

	public void setApelido(String apelido) {
		this.apelido = apelido;
	}

	public String getAgencia() {
		return agencia;
	}

	public void setAgencia(String agencia) {
		this.agencia = agencia;
	}

	public String getConta() {
		return conta;
	}

	public void setConta(String conta) {
		this.conta = conta;
	}

	public int getBanco() {
		return util.normalizeOptionToShow(banco);
	}

	public void setBanco(int banco) {
		this.banco = banco;
	}

	public int getSelectedIndex() {
		return selectedIndex;
	}
	
	public int getApplicationSelectedIndex(){
		return util.normalizeOptionToShow(service.getCurrentApplicationAccountIndex());
	}

	public void setApplicationSelectedIndex(int index){
		service.setApplicationAccountIndex(util.normalizeOptionToSave(index));
	}
	
	public String getApplicationAccountName(){
		return service.getApplicationAccountName();
	}
	
	public void setSelectedIndex(int selectedIndex) {
		this.selectedIndex = selectedIndex;
		loadData();
	}

	public String[][] getContas(){
		return service.getAll();
	}
	
	public String[] getNomes(){
		return util.includeSelectOption(service.getNomes(),"application.toolbar.filter.currentAccount.all");
	}
	
	public String[] getBancos(){
		return util.includeSelectOption(bancoService.getNomes());
	}

	public void callDel() throws StatizoException{
		service.delete(selectedIndex);
	}

	public boolean existConta() {
		return service.size() > 0;
	}
	
	public void setPadrao(boolean padrao) {
		this.padrao = padrao;
	}
	
	public boolean isPadrao(){
		return padrao;
	}
	
}